﻿Imports Controle
Imports UtilPadrao
Imports System.IO
Public Class frmEmpresa

#Region "Variáveis"

    Private objEmpresa As New ctrEmpresa
    Private sCaminhoLogomarca As String
    Private picByte() As Byte = Nothing

#End Region

#Region "Eventos"

    Private Sub frmAdicionarEmpresa_alteraModo(eModoAtual As FronteiraPadrao.frmPadrao.eModo) Handles Me.alteraModo
        Me.fraGeral.Enabled = eModoAtual = eModo.eAlterando
        Me.lblCod.Enabled = eModoAtual = eModo.eAguardando
        Me.txtCodigo.Enabled = eModoAtual = eModo.eAguardando
        Me.btnPesquisar.Enabled = eModoAtual = eModo.eAguardando
    End Sub

    Private Sub frmAdicionarEmpresa_gravar(ByRef bCancel As Boolean) Handles Me.gravar
        Dim dCnpj As Double
        Try
            bCancel = True
            dCnpj = conversao.toDouble(Me.TxtCNPJ.Text.Replace(".", "").Replace("/", "").Replace("-", "").Replace(",", ""))
            If Me.objEmpresa.gravarEmpresa(conversao.toInt32(Me.txtCodigo.Text),
                                           Me.txtRazaoSocial.Text,
                                           Me.txtNomeFantasia.Text,
                                           Me.UcEndereco1.CodLogradouro,
                                           dCnpj,
                                           Me.txtInsMunicipal.Text,
                                           Me.txtSite.Text,
                                           Me.txtEmail.Text,
                                           Me.picByte,
                                           Me.txtTelefone.Text,
                                           Me.txtInscEstadual.Text,
                                           Me.UcEndereco1.txtComplemento.Text,
                                           Me.UcEndereco1.txtNumero.Text) Then
                bCancel = False
                Me.chave = objEmpresa.codEmpresa
            Else
                Me.Mensagens = objEmpresa.retornaMensagensValidacao
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub frmAdicionarEmpresa_excluir(ByRef bCancel As Boolean) Handles Me.excluir

        Try

            bCancel = True

            If Me.objEmpresa.excluirEmpresa(conversao.nuloParaZero(Me.txtCodigo.Text)) Then
                bCancel = False
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub frmAdicionarEmpresa_carregaDados(ByVal iChave As Integer) Handles Me.carregaDados
        Dim bitmap As Bitmap
        Dim drEmpresa As DataRow
        Dim bits As Byte() = {0}
        Dim memorybits As MemoryStream

        Try

            If conversao.nuloParaZero(MyBase.chave) <> 0 Then

                drEmpresa = Me.objEmpresa.selecionarEmpresaCodigo(Me.chave)

                If Not IsNothing(drEmpresa) Then
                    With drEmpresa
                        Me.txtCodigo.Text = conversao.toInt32(.Item("codEmpresa"))
                        Me.txtRazaoSocial.Text = conversao.nuloParaVazio(.Item("razaoSocial"))
                        Me.txtNomeFantasia.Text = conversao.nuloParaZero(.Item("nomeFantasia"))
                        Me.TxtCNPJ.Text = Formatacao.formatCnpj(conversao.toDouble(.Item("cnpj")))
                        Me.txtInscEstadual.Text = conversao.nuloParaVazio(.Item("inscEstadual"))
                        Me.txtInsMunicipal.Text = conversao.nuloParaVazio(.Item("inscMunicipal"))
                        Me.txtSite.Text = conversao.nuloParaVazio(.Item("site"))
                        Me.txtEmail.Text = conversao.nuloParaVazio(.Item("email"))
                        Me.txtTelefone.Text = conversao.nuloParaVazio(.Item("telefone"))
                        Me.UcEndereco1.CodLogradouro = conversao.toInt32(.Item("codEndereco"))
                        Me.UcEndereco1.txtNumero.Text = conversao.nuloParaVazio(.Item("numero"))
                        Me.UcEndereco1.txtComplemento.Text = conversao.nuloParaVazio(.Item("complemento"))

                        If Not .Item("logo") Is System.DBNull.Value Then
                            bits = CType(.Item("logo"), Byte())
                            memorybits = New MemoryStream(bits)
                            bitmap = New Bitmap(memorybits)
                            Me.pctLogo.Image = bitmap
                            Me.pctLogo.SizeMode = PictureBoxSizeMode.StretchImage
                            Me.picByte = .Item("logo")
                        Else
                            Me.gerarLogomarcaImagem(GlobaisPadrao.Variaveis.byLogomarcaPadrao)
                        End If

                    End With
                Else
                    MessageBox.Show("Dados da empresa não encontrados.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    MyBase.chave = 0
                End If

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

    Private Sub cmdSelecionarLogo_Click(sender As System.Object, e As System.EventArgs) Handles cmdSelecionarLogo.Click
        Dim fs As FileStream


        Try
            Dim openDlg As OpenFileDialog = New OpenFileDialog()
            openDlg.Filter = "Image File (*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.bmp;*.gif;*.png"

            Dim filter As String = openDlg.Filter
            openDlg.Title = "Selecionar logomarca da empresa"

            If (openDlg.ShowDialog() = Windows.Forms.DialogResult.OK) Then

                Me.sCaminhoLogomarca = openDlg.FileName
                Me.pctLogo.Image = New System.Drawing.Bitmap(Me.sCaminhoLogomarca)
                Me.pctLogo.SizeMode = PictureBoxSizeMode.StretchImage

                fs = New FileStream(Me.sCaminhoLogomarca, FileMode.Open, FileAccess.Read)
                ReDim Me.picByte(fs.Length)
                fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
                fs.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub btnPesquisar_Click(sender As System.Object, e As System.EventArgs) Handles btnPesquisar.Click

        Dim datDados As New DataTable
        Dim objAuxiliar As Object()

        Try

            Me.objEmpresa = New ctrEmpresa
            datDados = Me.objEmpresa.selecionarEmpresa(0)

            If datDados.Rows.Count > 0 Then



                objAuxiliar = FronteiraPadrao.frmPesquisa.executarPesquisaGenerica("Selecionar Empresa",
                                                                                    Me.objEmpresa.sCamposTabela,
                                                                                    Me.objEmpresa.sCamposDemonstracao,
                                                                                    Me.objEmpresa.sCamposChave,
                                                                                    Me.objEmpresa.sTitulosColunas,
                                                                                    datDados)
                If Not Verificador.seVazio(objAuxiliar) Then
                    Me.chave = objAuxiliar(0)
                End If

            Else
                MessageBox.Show("Não existem registros cadastrados para a pesquisa.",
                                Me.Text,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub txtCodigo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtCodigo.KeyDown
        If e.KeyCode = Keys.Enter Then

            Me.chave = conversao.toInt32(Me.txtCodigo.Text)

        End If
    End Sub

    Private Sub frmAdicionarEmpresa_limpaCampo() Handles Me.limpaCampo
        MyBase.limpaCampos(Me)
    End Sub

#End Region

#Region "Métodos"

    Private Sub gerarLogomarcaImagem(ByVal byImagem As Byte())

        Dim bits As Byte() = {0}
        Dim memorybits As MemoryStream
        Dim bitmap As Bitmap

        bits = CType(byImagem, Byte())
        memorybits = New MemoryStream(bits)
        bitmap = New Bitmap(memorybits)
        Me.pctLogo.Image = bitmap
        Me.pctLogo.SizeMode = PictureBoxSizeMode.StretchImage
        Me.picByte = byImagem

    End Sub

    Private Sub frmEmpresa_inserir(ByRef bCancel As Boolean) Handles Me.inserir
        Me.gerarLogomarcaImagem(Uteis.byLogomarcaBranco)
    End Sub

#End Region


End Class